﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Common.Core;

namespace WebSite.Admin.Attribute
{
    /// <summary>
    /// 授权筛选器
    /// </summary>
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = false)]
    public sealed class AuthorizeFilterAttribute : ActionFilterAttribute
    {
        /// <summary>
        /// Action过滤器
        /// </summary>
        /// <param name="filterContext"></param>
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            //if (filterContext == null)
            //{
            //    ArgumentNullException exception = new ArgumentNullException();
            //    exception.LogError(exception.Message);
            //    filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(new { controller = "Login", action = "Login" }));
            //}

            //var dataCollection = filterContext.HttpContext.Session["DataCollection"] as DataCollection;
            //if (dataCollection == null)
            //{
            //    filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(new { controller = "Login", action = "Login" }));
            //}
            //else
            //{
            //    if (dataCollection.Employee.IsSys == 1) return;
            //    var path = filterContext.HttpContext.Request.Path;
            //    if (path.Equals("/", StringComparison.CurrentCultureIgnoreCase)) return;
            //    var controllerName = filterContext.RouteData.Values["controller"].ToString();
            //    var actionName = filterContext.RouteData.Values["action"].ToString();
            //    //var module = PermissionService.GetModuleModel(controllerName, actionName, dataCollection.Employee.CompanyId);
            //    var module = dataCollection.Modules.FirstOrDefault(c => c.ControllerName == controllerName && c.ActionName == actionName);
            //    if (module == null || !module.IsAuthorize) return;
            //    PermissionEnum permission = PermissionService.CheckPermission(dataCollection.Modules, module);
            //    switch (permission)
            //    {
            //        case PermissionEnum.PageError:
            //            filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(new { controller = "Shared", action = "Error" }));
            //            break;
            //        case PermissionEnum.ButtonError:
            //            filterContext.Result = new JsonResult() { Data = new { status = "error", message = "你无此操作权限" }, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
            //            break;
            //        default:
            //            break;
            //    }
            //}
        }
    }
}